//
//  XYCalculatorModel.m
//  Calculator
//
//  Created by Xia Yong on 12-12-5.
//  Copyright (c) 2012年 Xia Yong. All rights reserved.
//

#import "XYCalculatorModel.h"

@interface XYCalculatorModel()
- (NSNumber *) lastOfNumber;
@end

@implementation XYCalculatorModel

@synthesize data = _data;

- (NSMutableArray *) data {
    if(!_data) {
        _data = [NSMutableArray arrayWithCapacity:2];
    }
    return _data;
}

- (void) addNumber:(NSNumber *)number {
    [self.data addObject:number];
}

- (double) calculate:(NSString *)sign {
    double result = 0;
    if ([sign isEqualToString:@"+"]) {
        result = [[self lastOfNumber] doubleValue] + [[self lastOfNumber] doubleValue];
    }
    else if ([sign isEqualToString:@"-"]) {
        result = [[self lastOfNumber] doubleValue] - [[self lastOfNumber] doubleValue];
    }
    else if ([sign isEqualToString:@"*"]) {
        result = [[self lastOfNumber] doubleValue] * [[self lastOfNumber] doubleValue];
    }
    else if ([sign isEqualToString:@"/"]) {
        result = [[self lastOfNumber] doubleValue] / [[self lastOfNumber] doubleValue];
    }
    return result;
}

- (NSNumber *) lastOfNumber {
    NSNumber *lastNumber = [self.data lastObject];
    if (lastNumber != nil) {
        [self.data removeLastObject];
    }
    return lastNumber;
}

@end
